package sg4c.protocol;

import java.io.ObjectInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;

public class CapturedInputProtocol implements DataInputProtocol {

    private final InputStream in;
    private final ObjectInputStream dis;
    private int lastInteger;

    public CapturedInputProtocol(InputStream is) throws IOException {
        this.in = is;
        //this.in = new GZIPInputStream(is);
        this.dis = new ObjectInputStream(in);
    }

    public int readInt() throws IOException {
        lastInteger = dis.readInt();
        return lastInteger;
    }

    public float readFloat() throws IOException {
        return dis.readFloat();
    }

    public double readDouble() throws IOException {
        return dis.readDouble();
    }

    public String readString() throws IOException {
        byte[] buf = new byte[lastInteger];
        dis.readFully(buf);
        return new String(buf);
    }

    public Object readObject() throws IOException{
        try {
            return dis.readObject();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
            System.exit(1);
            return null;
        }
    }

    public void next() throws IOException {
        //dummy
    }
}
